package server.business.service.Impl;

import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.ReturnBloodStationMapper;
import server.business.pojo.ReturnBloodStation;
import server.business.service.ReturnBloodStationService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Galaxy
 * @description 针对表【return_blood_station(血液退回血站信息表)】的数据库操作Service实现
 * @createDate 2022-11-08 12:46:24
 */
@Service("returnBloodStationService")
public class ReturnBloodStationServiceImpl extends ServiceImpl<ReturnBloodStationMapper, ReturnBloodStation>
        implements ReturnBloodStationService {
    /**
     * 使用数据库查询
     */
    @Autowired
    private ReturnBloodStationMapper returnBloodStationMapper;

    /**
     * 引入血液退回血站Service
     */
    @Autowired
    private ReturnBloodStationService returnBloodStationService;

    /**
     * 查询所有血液退回血站信息
     *
     * @return 返回血液退回血站信息列表
     */
    @Override
    public List<ReturnBloodStation> findAll() {
        return returnBloodStationMapper.findAll();
    }

    /**
     * 插入血站退血信息
     *
     * @param returnBloodStation 血液退回血站包装类
     */
    @Override
    public void insertBackInformation(ReturnBloodStation returnBloodStation) {
        returnBloodStationMapper.insert(returnBloodStation);
    }

    /**
     * 根据待退血单号修改血液状态
     *
     * @param number    待退血单号
     * @param condition 待修改信息
     */
    @Override
    public void setConditionByNumber(String number, String condition) {
        returnBloodStationMapper.setConditionByNumber(number, condition);
    }

    /**
     * 根据退血单号修改退血信息
     *
     * @param number             退血单号
     * @param returnBloodStation 血站退血信息包装类
     */
    @Override
    public void updateReturnStationByNumber(String number, ReturnBloodStation returnBloodStation) {
        returnBloodStationMapper.updateReturnStationByNumber(number, returnBloodStation);
    }

    /**
     * 根据退血单号删除退血信息
     *
     * @param number 退血单号
     */
    @Override
    public void deleteReturnStationByNumber(String number) {
        returnBloodStationMapper.deleteReturnStationByNumber(number);
    }

    /**
     * 根据退血单号设置审批人信息以及状态
     *
     * @param number        退血单号
     * @param auditor       审批人姓名
     * @param auditorNumber 审批人工号
     * @param auditTime     审批时间
     * @param condition     状态
     */
    @Override
    public void setAuditAndConditionByNumber(String number, String auditor, String auditorNumber, Date auditTime, String condition) {
        returnBloodStationMapper.setAuditAndConditionByNumber(number, auditor, auditorNumber, auditTime, condition);
    }

    /**
     * 返回所有审批通过的血站退血信息
     *
     * @return 返回所有审批通过的血站退血信息
     */
    @Override
    public List<ReturnBloodStation> backInformationByCondition() {
        return returnBloodStationMapper.backInformationByCondition();
    }

    /**
     * 根据编号批量下载选中数据
     *
     * @param response 请求
     * @param ids      编号列表
     * @throws IOException 异常
     */
    @Override
    public void batchDownload(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
        List<ReturnBloodStation> returnBloodStations = returnBloodStationMapper.selectBatchIds(ids);
        // 设置文本内容
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("血液退回血站信息" + System.currentTimeMillis(), "UTF-8");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream(),
                    ReturnBloodStation.class).sheet("血液退回血站信息")
                    .doWrite(returnBloodStations);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 分页查询功能
     *
     * @param currentPage        当前页面
     * @param pageSize           页面显示个数
     * @param returnBloodStation 血站退血信息
     * @return 返回分页数据
     */
    @Override
    public IPage<ReturnBloodStation> getPage(int currentPage, int pageSize, ReturnBloodStation returnBloodStation) {
        LambdaQueryWrapper<ReturnBloodStation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(returnBloodStation.getBloodId()), ReturnBloodStation::getBloodId, returnBloodStation.getBloodId())
                .eq(Strings.isNotBlank(returnBloodStation.getBackInformationNumber()), ReturnBloodStation::getBackInformationNumber, returnBloodStation.getBackInformationNumber())
                .eq(Strings.isNotBlank(returnBloodStation.getApplicantName()), ReturnBloodStation::getApplicantName, returnBloodStation.getApplicantName())
                .like(returnBloodStation.getApplyTime() != null, ReturnBloodStation::getApplyTime, returnBloodStation.getApplyTime())
                .eq(Strings.isNotBlank(returnBloodStation.getState()), ReturnBloodStation::getState, returnBloodStation.getState())
                .like(returnBloodStation.getAuditTime() != null, ReturnBloodStation::getAuditTime, returnBloodStation.getAuditTime())
                .eq(Strings.isNotBlank(returnBloodStation.getAuditorNumber()), ReturnBloodStation::getAuditorNumber, returnBloodStation.getAuditorNumber());
        Page<ReturnBloodStation> page = new Page<>(currentPage, pageSize);
        returnBloodStationMapper.selectPage(page, queryWrapper);
        return page;
    }

    /**
     * 根据编号删除
     *
     * @param ids 待删除数据的编号
     * @return 返回删除结果
     */
    @Override
    public boolean deleteBatch(ArrayList<Long> ids) {
        boolean res = returnBloodStationService.removeByIds(ids);
        return res;
    }

    /**
     * 分页查询功能
     *
     * @param currentPage        当前页面
     * @param pageSize           页面显示个数
     * @param returnBloodStation 血站退血信息
     * @return 返回分页数据
     */
    @Override
    public IPage<ReturnBloodStation> getPageByState(int currentPage, int pageSize, ReturnBloodStation returnBloodStation) {
        LambdaQueryWrapper<ReturnBloodStation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(returnBloodStation.getBloodId()), ReturnBloodStation::getBloodId, returnBloodStation.getBloodId())
                .eq(Strings.isNotBlank(returnBloodStation.getBackInformationNumber()), ReturnBloodStation::getBackInformationNumber, returnBloodStation.getBackInformationNumber())
                .eq(Strings.isNotBlank(returnBloodStation.getApplicantName()), ReturnBloodStation::getApplicantName, returnBloodStation.getApplicantName())
                .like(returnBloodStation.getApplyTime() != null, ReturnBloodStation::getApplyTime, returnBloodStation.getApplyTime())
                .eq(Strings.isNotBlank(returnBloodStation.getState()), ReturnBloodStation::getState, "24")
                .like(returnBloodStation.getAuditTime() != null, ReturnBloodStation::getAuditTime, returnBloodStation.getAuditTime())
                .eq(Strings.isNotBlank(returnBloodStation.getAuditorNumber()), ReturnBloodStation::getAuditorNumber, returnBloodStation.getAuditorNumber());
        Page<ReturnBloodStation> page = new Page<>(currentPage, pageSize);
        returnBloodStationMapper.selectPage(page, queryWrapper);
        return page;
    }
}




